package com.crane.oss.sports;

import com.crane.common.framework.http.apache.HttpClientUtils;
import com.crane.common.framework.http.apache.HttpResult;
import com.crane.common.framework.utils.Jackson2JsonUtil;
import com.crane.oss.sys.service.impl.ApiServiceImpl;
import com.crane.oss.xunfei.req.ApplySchoolReq;
import com.crane.oss.xunfei.req.UpdateEventReq;
import com.crane.oss.xunfei.resp.ApiSingleResponse;
import com.fasterxml.jackson.core.type.TypeReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 推送基础信息变化接口
 */
@Component
public class XunFeiPushEventApiService extends XunFeiApiService {


    private static final Logger logger = LoggerFactory.getLogger(XunFeiPushEventApiService.class);

    public static final String EVENT_CAMPUS="campus";
    public static final String EVENT_GRADE_CLASS="gradeClass";
    public static final String EVENT_STUDENT="student";
    public static final String EVENT_TEACHER="teacher";
    public static final String EVENT_REF_STUDENT_CLASS="refStudentClass";
    public static final String EVENT_TEACH_CYCLE="teachCycle";



    public static final String INSERT="insert";
    public static final String UPDATE="update";
    public static final String DELETE="delete";

    public ApiSingleResponse applySchool(ApplySchoolReq request, String sid, String did, String uid) {
        Map<String, String> params = getStringStringMap(request, sid, did, uid);
        HttpResult httpResult= HttpClientUtils.post(xunFeiAppConfig.getUrl()+"/apply/school",params);
        if(httpResult.isSuccess()){
            ApiSingleResponse response=httpResult.getDataObjectByJson(new TypeReference<ApiSingleResponse>(){});
            return response;
        }
        return ApiSingleResponse.buildFailure();
    }


    public ApiSingleResponse updateEvent(UpdateEventReq request, String sid, String did, String uid) {
        Map<String, String> params = getStringStringMap(request, sid, did, uid,"body");
        StringBuilder url=new StringBuilder();
        int i=0;
        params.forEach((key,value)->{
            if(!key.equals("sign") && !key.equals("body")){
                url.append(key).append("=").append(value).append("&");
            }
        });
        url.append("sign="+params.get("sign"));
        HttpResult httpResult= HttpClientUtils.postJson(xunFeiAppConfig.getUrl()+"/update/event?"+url,params.get("body"));
        logger.info("updateEvent req={},resp={}", Jackson2JsonUtil.toString(request),httpResult.getDataStringUTF8());
        if(httpResult.isSuccess()){
            ApiSingleResponse response=httpResult.getDataObjectByJson(new TypeReference<ApiSingleResponse>(){});
            return response;
        }
        return ApiSingleResponse.buildFailure();
    }

}

